package com.example.entity;

import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/1/26
 * @time: 14:59
 */
@Data
public class SelectOptionsList {

    private List<Map> indentOptionsList;

    /**
     * 通过treeOptionsList 生成 indentOptionsList
     *
     * @param treeOptionsList
     */
    public void makeIndentOptions(List<TreeOptions> treeOptionsList) {
        // 整理
        List<Map> indentOptionsList = new ArrayList<>();
        Map root = new HashMap();
        root.put("code", -1);
        root.put("title", "根节点");
        root.put("deleted", 0);
        indentOptionsList.add(root);
        indentOptionsList.addAll(recursiveIndentation("|--", treeOptionsList));
        // set 值
        setIndentOptionsList(indentOptionsList);
    }

    /**
     * 递归缩进，整理成带缩进前缀的列表
     *
     * @param indentation
     * @param chlidList
     * @return
     */
    private List<Map> recursiveIndentation(String indentation, List<TreeOptions> chlidList) {
        List<Map> indentOptionsList = new ArrayList<>();
        for (int i = 0; chlidList != null && i < chlidList.size(); i++) {
            Map map = new HashMap();
            map.put("code", chlidList.get(i).getCode());
            map.put("title", indentation + chlidList.get(i).getTitle());
            map.put("deleted", chlidList.get(i).getDeleted());
            indentOptionsList.add(map);
            indentOptionsList.addAll(recursiveIndentation(indentation + "--", chlidList.get(i).getChildren()));
        }
        return indentOptionsList;
    }
}
